from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
import requests

from app_weather.models import QueryWeather


def query_weather_views(request):
    # print(request)
    city =request.GET.get('city')
    # print(city)
    result = query_weather(city)
    print(result)
    # ccc = result.get('城市')
    # print(ccc)
    return HttpResponse(result)

def query_weather(city):
    # 查询城市的天气
    # 1、校验城市名称是否合法
    if isinstance(city,str):

    # 2、调用天气接口
    # 3、Python 请求接口+
        url = 'https://www.yiketianqi.com/free/day?appid=91634916&appsecret=Qa6YrUvV&unescape=1&city='+city


        r = requests.get(url,city)
        re = r.json()
        city = re.get('city')
        wea = re.get('wea')
        week = re.get('week')
        update_time = re.get('update_time')
        tem = re.get('tem')
        tem_day = re.get('tem_day')
        date = re.get('date')
        tem_night = re.get('tem_night')
        win = re.get('win')
        win_speed = re.get('win_speed')
        win_meter = re.get('win_meter')
        air = re.get('air')


        save_data_model = QueryWeather()
        save_data_model.city = re.get('city')
        save_data_model.tem_day = wea
        save_data_model.weather = re.get('wea')
        save_data_model.week = re.get('week')
        save_data_model.tem = re.get('tem')
        save_data_model.tem_night = re.get('tem_night')
        save_data_model.win = re.get('win')
        save_data_model.win_speed = re.get('win_speed')
        save_data_model.win_meter = re.get('win_meter')
        save_data_model.air = re.get('air')
        save_data_model.date = re.get('date')
        save_data_model.update_time = re.get('update_time')

        save_data_model.save()
        # QueryWeather.objects.all().values()
        print(123)

        result = '城市：{}，天气：{}，今天是：{},更新时间: {},温度: {},白天气温: {},日期: {},最低温度: {},风向: {},等级: {},风速: {},空气质量:{}'.format(city, wea, week,update_time,tem,tem_day,date,tem_night,win,win_speed,win_meter,air)
        return result

        # date = re.get('date')
        # week = re.get('week')
        # update_time = re.get('update_time')
        # wea = re.get('wea')
        # tem = re.get('tem')
        # tem_day = re.get('tem_day')
        # tem_night = re.get('tem_night')
        # win = re.get('win')
        # win_speed = re.get('win_speed')
        # win_meter = re.get('win_meter')
        # air = re.get('air')
    else:
        return '输入的城市名称不合法'
#
# result = query_weather('遂宁')
# print(result)

# def SaveData_View(request):
#
#         city = request.GET.get('city')
#         result = query_weather(city)
#         save_data_model = QueryWeather()
#         save_data_model.city = result.GET('城市')
#         save_data_model.date = request.GET['日期']
#         save_data_model.week = request.GET['今天是']
#         # save_data_model.update_time = request.GET['update_time']
#         # save_data_model.weather = request.GET['weather']
#         # save_data_model.tem = request.GET['tem']
#         # save_data_model.date = request.POST['date']
#         # save_data_model.date = request.POST['date']
#         save_data_model.save()
#         # 可以使用这行代码确认是否添加成功：
#         QueryWeather.objects.all().values()
#         return HttpResponse('get')

    # def get(request):
    #     # city = request.GET.get('city')
    #     # result = query_weather(city)
    #     save_data_model = QueryWeather()
    #     save_data_model.city = request.GET['city']
    #     save_data_model.date = request.GET['date']
    #     save_data_model.week = request.GET['week']
    #     save_data_model.update_time = request.GET['update_time']
    #     save_data_model.weather = request.GET['weather']
    #     save_data_model.tem = request.GET['tem']
    #     # save_data_model.date = request.POST['date']
    #     # save_data_model.date = request.POST['date']
    #     save_data_model.save()
    #     # 可以使用这行代码确认是否添加成功：
    #     # QueryWeather.objects.all().values()
    #     return HttpResponse('get')


    # def post(self,request):
    #
    #         save_data_model = QueryWeather()
    #         save_data_model.city = request.POST['city']
    #         save_data_model.date = request.POST['date']
    #         save_data_model.week = request.POST['week']
    #         save_data_model.update_time = request.POST['update_time']
    #         save_data_model.weather = request.POST['weather']
    #         save_data_model.tem = request.POST['tem']
    #         # save_data_model.date = request.POST['date']
    #         # save_data_model.date = request.POST['date']
    #         save_data_model.save()
    #         # 可以使用这行代码确认是否添加成功：
    #         # QueryWeather.objects.all().values()
    #         return HttpResponse('post')




    # if   request.method == "GET":
    #     get()
    #
    # elif request.method == "POST":
    #     print("uname is ", request.POST['uname'])
    #     return HttpResponse("POST is ok")
    # else:
    #     pass




    # ccc = result.get('城市')
        # print(ccc)




